﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SelfInfo.Models;
using System.Data.Entity.Infrastructure;
using System.Data.Entity;
using Webdiyer.WebControls.Mvc;



namespace SelfInfo.Controllers
{
   
    public class ManageController : Controller
    {
        private SelfInfoEntities SelfInfo = new SelfInfoEntities();

        public ActionResult IndexItem()
        {
            IList<ITEM> item = SelfInfo.ITEM.ToList<ITEM>();
        
            return View(item);
        }

        public ActionResult CreateItem()
        {

            return View();
        }

        [HttpPost]
        public ActionResult CreateItem(TYPE item)
        {
           
                //example
                //创建teacher一个实体
                //ITEM t = new ITEM();
                //t.TYPE = item.type;
                //t.NAME = "BOOK";
                //e.ITEM.AddObject(item);
                ////写回数据库
                //e.SaveChanges();

            if (ModelState.IsValid)
            {
                SelfInfo.ITEM.AddObject(item);
                SelfInfo.SaveChanges();
                return RedirectToAction("IndexItem");
            }

            return View(item);
        
        }


        public ActionResult IndexPiece()
        {
            IList<PIECE> piece = SelfInfo.PIECE.ToList<PIECE>();
            return View(piece);
        }

        public ActionResult CreatePIECE()
        {
            var query = SelfInfo.ITEM.Select(c => new { c.ID, c.NAME });
            ViewData["TYPE"] = new SelectList(query.AsEnumerable(), "ID", "NAME"); 
            return View();
        }

        [HttpPost]
        public ActionResult CreatePiece(PIECE piece)
        {

            //example
            //创建teacher一个实体
            //ITEM t = new ITEM();
            //t.TYPE = item.type;
            //t.NAME = "BOOK";
            //e.ITEM.AddObject(item);
            ////写回数据库
            //e.SaveChanges();

            if (ModelState.IsValid)
            {
                SelfInfo.PIECE.AddObject(piece);
                SelfInfo.SaveChanges();
                return RedirectToAction("IndexPiece");
            }

            return View(piece);

        }



        //-------------------------------


        public ActionResult IndexSelfInfo(int id=1)
        {
            IList<SELFINFO> sinfo = SelfInfo.SELFINFO.OrderByDescending(c => c.TODAY).ToPagedList <SELFINFO>(id, 10);//.ToList<SELFINFO>();
      
            return View(sinfo);
        }

        public ActionResult CreateSelfInfo()
        {
            SELFINFO si = new SELFINFO();
            si.TODAY = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd"));
            var query = SelfInfo.PIECE.Select(c => new { c.ID, c.NAME });
            ViewData["PIECEID"] = new SelectList(query.AsEnumerable(), "ID", "NAME"); ;
            return View(si);
        }

        [HttpPost]
        public ActionResult CreateSelfInfo(SELFINFO sinfo)
        {
            if (ModelState.IsValid)
            {
                SelfInfo.SELFINFO.AddObject(sinfo);
                SelfInfo.SaveChanges();
                return RedirectToAction("IndexSelfInfo");
            }

            return View(sinfo);

        }

        public ActionResult EditSelfInfo(int id)
        {

            IQueryable<SELFINFO> si = SelfInfo.SELFINFO
                           .Where(q => q.ID == id);
          
            return View(si.First<SELFINFO>());
        }

        [HttpPost]
        public ActionResult EditSelfInfo(SELFINFO sinfo)
        {
            //SELFINFO si = SelfInfo.SELFINFO.First(i => i.ID == sinfo.ID);
            SelfInfo.SELFINFO.Attach(sinfo);
            SelfInfo.ObjectStateManager.ChangeObjectState(sinfo,System.Data.EntityState.Modified);
            SelfInfo.SaveChanges();
            return RedirectToAction("IndexSelfInfo");
        }
    }
}
